package org.xteam.sled.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.xteam.sled.semantic.exp.Exp;
import org.xteam.sled.semantic.exp.VariableSubstitute;

public class LabelledDisjunct extends BaseDisjunct<LabelledSequence> {

	public LabelledDisjunct(LabelledSequence seq) {
		super(null, new HashSet<Exp>(), seq);
	}

	public LabelledDisjunct(String name, Set<Exp> conditions,
			LabelledSequence seq) {
		super(name, conditions, seq);
	}

	public AbsoluteProtoDisjunct anf() {
		Map<AbsoluteField, Exp> bindings = new HashMap<AbsoluteField, Exp>();
		AbsoluteSequence seq = sequence.anf(bindings);
		return new AbsoluteProtoDisjunct(name, conditions, seq, bindings);
	}
	
	@Override
	public IDisjunct and(IDisjunct other) {
		return (LabelledDisjunct)super.and(other);
	}

	@Override
	public IDisjunct seq(IDisjunct other) {
		return (LabelledDisjunct)super.seq(other);
	}

	@Override
	public IDisjunct postDots() {
		return (LabelledDisjunct)super.postDots();
	}

	@Override
	public IDisjunct preDots() {
		return (LabelledDisjunct)super.preDots();
	}

	@Override
	protected BaseDisjunct<LabelledSequence> createDisjunct(String name, Set<Exp> conds,
			ISequence seq) {
		return new LabelledDisjunct(name, conds, (LabelledSequence)seq);
	}

	public LatentDisjunct makeLabelsPCRelative() {
		VariableSubstitute subs = new VariableSubstitute(sequence.labelOffsets());
  		Set<Exp> newConditions = new HashSet<Exp>();
  		for (Exp condition : conditions) {
  			newConditions.add(subs.rewrite(condition));
		}
  		return new LatentDisjunct(name, newConditions, sequence.makePCRelative(subs));
	}

	public LabelledDisjunct actualizeLatent(boolean actualsOk) {
		return new LabelledDisjunct(name, new HashSet<Exp>(conditions), sequence.actualize(actualsOk));
	}

}
